//
//  PasswordInputVC.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/16.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "PasswordInputVC.h"

#import "Macro.h"
#import "UIView+DDAddition.h"
#import "TimerView.h"
#import "PasswordView.h"
#import "EPColor.h"
#import "EPButton.h"
#import "NotificationHelp.h"
#import "AccountSysVM.h"
#import "UserInfoModel.h"
#import "CommonHelper.h"
#import "UserSettings.h"

@interface PasswordInputVC ()

// 静态控件
@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) UILabel *lblMessage;
@property (nonatomic, strong) UILabel *lblPasswordRule;
// 动态控件
@property (nonatomic, strong) PasswordView *passwordView;
@property (nonatomic, strong) EPButton *btnNext;
@property (nonatomic, assign) BOOL isNextOpt;
@property (nonatomic, assign) BOOL isQuickLogin;

// 数字
@property (nonatomic, copy) NSString *strAreaCode;
@property (nonatomic, copy) NSString *strMobile;
@property (nonatomic, copy) NSString *strSmsCode;

@end

@implementation PasswordInputVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addSubviews];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.lblPasswordRule.text = @"密码为6-16位，包含字母或数字";
    if (ForForgotType == self.type) {
        self.lblPasswordRule.text = @"密码为6-16位，且同时包含字母和数字";
    }
    // 找回密码允许右上角点跳过   2018.9.10 JX 目前不需要跳过
//    if (ForForgotType == self.type) {
//        [self createNavBarRightButtonWithName:@"跳过"];
//
//        WEAKSELF
//        self.rightNavButtonBlock = ^{
//            UserSaveInfo.token = weakSelf.tempToken;
//            [weakSelf showProcessingHud:@"..."];
//            [(AccountSysVM *)weakSelf.baseVM requestUserInfoForQuickLoginWithAreaCode:weakSelf.strAreaCode andMobile:weakSelf.strMobile];
//        };
//        self.lblPasswordRule.text = NSLocalizedString(@"RetrievePasswordRole", @"需6-16位密碼且同時包含字母和數字");
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 接口调用返回结果处理
- (void)handelRequestData:(BaseJSONEntity *)returnValue
{
    [self hideProcessingHud];
    if (0 != returnValue.ret) {
        [self showAlertWithTitle:returnValue.msg];
        return;
    }
    
    // 区分下一步和跳过操作
    if (self.isNextOpt) {
        self.isNextOpt = NO;
        UserSaveInfo.WXOpenID = @"";
        UserSaveInfo.WXAccessToken = @"";
        UserSaveInfo.FBOpenID = @"";
        UserSaveInfo.FBAccessToken = @"";
        // 保存用户信息
//        NSDictionary *userInfo = [returnValue.data objectForKey:@"user_info"];
        [NotificationHelp postNotification:NotificationLoginSuccess object:returnValue.data];
        NSString *token = [returnValue.data objectForKey:@"d-tk"];
        if (token && 0 != token.length) UserSaveInfo.token = token;
        UserSaveInfo.areaCode = self.strAreaCode;
        UserSaveInfo.mobile = self.strMobile;
        [NotificationHelp postNotification:NotificationGo2MainTBC object:nil];
        return;
    }
    else
    {
        // 跳过的场景，需要重新获取用户信息
        if (!self.isQuickLogin) {
            self.isQuickLogin = YES;
            UserSaveInfo.areaCode = self.strAreaCode;
            UserSaveInfo.mobile = self.strMobile;
            [self showProcessingHud:@"..."];
            [(AccountSysVM *)self.baseVM getUserInfoRequestWithAreaCode:self.strAreaCode andMobile:self.strMobile];
        }
        else
        {
            [NotificationHelp postNotification:NotificationLoginSuccess object:returnValue.data];
            [NotificationHelp postNotification:NotificationGo2MainTBC object:nil];
        }
    }
}

- (void)addSubviews
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lblTitle];
    self.lblTitle.top = 16.0f;
    [self.view addSubview:self.lblMessage];
    self.lblMessage.top = self.lblTitle.bottom + 8.f;
    [self.view addSubview:self.passwordView];
    self.passwordView.top = self.lblMessage.bottom + 47.f;
    [self.view addSubview:self.lblPasswordRule];
    self.lblPasswordRule.top = self.passwordView.bottom + Cell_X;
    [self.view addSubview:self.btnNext];
    self.btnNext.top = self.lblPasswordRule.bottom + Cell_X;
    
    WEAKSELF
    self.passwordView.btnClearBlock = ^{
        weakSelf.btnNext.enabled = NO;
    };
    self.passwordView.txfBecomeBlock = ^(NSInteger length) {
        weakSelf.btnNext.enabled = (6 <= length && length <=20)?YES:NO;
    };
}

#pragma mark - 懒加载
- (UILabel *)lblTitle
{
    if (!_lblTitle) {
        _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 32.f)];
        _lblTitle.text = @"请设置密码";
    }
    return _lblTitle;
}

- (UILabel *)lblMessage
{
    if (!_lblMessage) {
        _lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(LoginVC_X, 0, 200, 17.f)];
        _lblMessage.font = SystemFont(12.f);
        _lblMessage.text = @"登录密码用于移动端与电脑端登录";
    }
    return _lblMessage;
}

#pragma mark -- 密码区域
- (PasswordView *)passwordView
{
    if (!_passwordView) {
        _passwordView = [[PasswordView alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, 76.f)];
        _passwordView.textColor = [UIColor blackColor];
        _passwordView.type = PasswordShowType_Plaintext;
    }
    return _passwordView;
}

#pragma mark - 密码规则
- (UILabel *)lblPasswordRule
{
    if (!_lblPasswordRule) {
        _lblPasswordRule = [[UILabel alloc] initWithFrame:CGRectMake(LoginVC_X, 0, 300.f, 20.f)];
        _lblPasswordRule.font = SystemFont(12.f);
        _lblPasswordRule.textAlignment = NSTextAlignmentLeft;
        _lblPasswordRule.textColor = [EPColor epColor_Blue];
    }
    return _lblPasswordRule;
}

- (EPButton *)btnNext
{
    if (!_btnNext) {
        _btnNext = [[EPButton alloc] initWithFrame:CGRectMake(LoginVC_X, 0, LoginVC_W, Cell_H)];
        [_btnNext addTarget:self action:@selector(btnNextStepClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnNext setTitle:@"下一步" forState:UIControlStateNormal];
        [_btnNext buttonWithStyle:EPBasicButton_btn2];
        _btnNext.enabled = NO;
    }
    return _btnNext;
}

- (void)btnNextStepClick:(id)sender
{
    self.isNextOpt = YES;
    NSString *password = [self.passwordView getPassword];
    [self showProcessingHud:@"..."];
    
    self.baseVM = [[AccountSysVM alloc] init];
    [self createViewModel];
    if (ForRegisterType == self.type) {
        [(AccountSysVM *)self.baseVM checkPwdForRegisterWithAreaCode:self.strAreaCode andMobile:self.strMobile andSmsCode:self.strSmsCode andPwd:password];
    }
    else if (ForForgotType == self.type) {
        [(AccountSysVM *)self.baseVM checkPwdForRetrievePwdWithAreaCode:self.strAreaCode andMobile:self.strMobile andSmsCode:self.strSmsCode andPwd:password];
    }
    else if (ForBindingMobileTypeFromWX == self.type) {
        [(AccountSysVM *)self.baseVM checkPwdForRegisterFromWXWithAreaCode:self.strAreaCode andMobile:self.strMobile andSmsCode:self.strSmsCode andPwd:password andOpenId:UserSaveInfo.WXOpenID andAccessToken:UserSaveInfo.WXAccessToken];
    }
    else if (ForBindingMobileTypeFromFB == self.type) {
        [(AccountSysVM *)self.baseVM checkPwdForRegisterFromFBWithAreaCode:self.strAreaCode andMobile:self.strMobile andSmsCode:self.strSmsCode andPwd:password andOpenId:UserSaveInfo.FBOpenID andAccessToken:UserSaveInfo.FBAccessToken];
    }
}

- (void)setAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode
{
    self.strAreaCode = areaCode;
    self.strMobile = mobile;
    self.strSmsCode = smsCode;
}

@end
